using System;
using System.Linq;
using System.Reflection;
using UnityEngine;
using FairyGUI;
namespace ABA
{
    public static class Comm
    {
        public static void RandomSeed(int seed)
        {
            UnityEngine.Random.InitState(seed);
        }
        //[min, max)
        public static int RandomRangeInt(int min, int max)
        {
            var r = UnityEngine.Random.Range(min, max);
            return r;
        }
        //[min, max]
        public static float RandomRangeFloat(float min, float max)
        {
            var r = UnityEngine.Random.Range(min, max);          
            return r;
        }

        public static Vector2 RandomInsideCircle(float radius)
        {
            return UnityEngine.Random.insideUnitCircle * radius;
        }

        // 这个fucName 方法的脚本要放在带动画Animation的物体上 要不找不到回调方法
        // time  => -1 结束回调
        public static void AddAnimationEvent(Animation ani, string aniName, string fucName, float time){
            AnimationClip clip = ani.GetClip(aniName);
            AnimationEvent animationEvent = new AnimationEvent();
            animationEvent.functionName = fucName;
            //传入参数
            //animationEvent.objectReferenceParameter = target;
            //设置触发帧
            animationEvent.time = time;
            if(time == -1)
                animationEvent.time = clip.length;
            //注册事件
            clip.AddEvent(animationEvent);
        }

        public static Vector2 WorldPosToFGUIPos(Vector3 worldPos)
        {
            Vector3 screenPos = Camera.main.WorldToScreenPoint(worldPos);
            //原点位置转换
            screenPos.y = Screen.height - screenPos.y;
            Vector2 pt = GRoot.inst.GlobalToLocal(screenPos);

            return pt;
        }
        // 弧度(0.1) -> 角度(90)
        public static float RADIANS_TO_DEGREES(float radians){
            return radians * 180 / Mathf.PI;
        }
        public static float DEGREES_TO_RADIANS(float angle){
            return angle * Mathf.PI / 180;
        }
        // 返回两向量减的a - b后的向量的弧度,有方向 [-1,1]
        public static float GetSubVecRadians(Vector3 a, Vector3 b){
            Vector3 dir = a - b;
            var angleRadians = GetVecRadians(dir);
            return angleRadians;           
        }
        // 返回向量a的弧度, 有方向
        public static float GetVecRadians(Vector3 a){
            var angleRadians = Mathf.Atan2(a.y, a.x); 
            return angleRadians;             
        }
        // 得到点b->a的向量的相对于dis的x,y的增量
        public static Vector3 GetVecDeltaVec(Vector3 a, Vector3 b, float dis){
            var angleRadians = GetSubVecRadians(a, b);
            return new Vector3(Mathf.Cos(angleRadians) * dis, Mathf.Sin(angleRadians) * dis, 0);             
        }
        public static float GetVecRotationRadians(Vector3 from, Vector3 to, Vector3 zhou){
            var r = Vector3.SignedAngle(from, to, zhou);
            return DEGREES_TO_RADIANS(r);
        }
        // 向量 from -> to 的旋转角度 zhou: 旋转轴
        public static float GetVecRotationDegress(Vector3 from, Vector3 to, Vector3 zhou) {
            var r = Vector3.SignedAngle(from, to, zhou);
            return r;
        }

        public static long GetNowTimeSec(){
            return DateTimeOffset.Now.ToUnixTimeSeconds();
        }

        public static TimeSpan GetTime2Now(long time){
            var timeUnnix = DateTimeOffset.FromUnixTimeSeconds(time);
            return new TimeSpan(DateTimeOffset.UtcNow.Ticks - timeUnnix.Ticks);
        }

        public static TimeSpan GetTime2Time(long time1,long time2){
            var timeUnnix1 = DateTimeOffset.FromUnixTimeSeconds(time1);
            var timeUnnix2 = DateTimeOffset.FromUnixTimeSeconds(time2);
            return new TimeSpan(timeUnnix1.Ticks - timeUnnix2.Ticks);
        }

        public static string FormatTime(TimeSpan t){
            string ret = "";
            if(t.Days > 0){
                ret = string.Format("{0}天{1}小时{2}分{3}秒",t.Days,t.Hours,t.Minutes,t.Seconds);
            }
            else if(t.Hours > 0){
                ret = string.Format("{0}小时{1}分{2}秒",t.Hours,t.Minutes,t.Seconds);
            }
            else if(t.Minutes > 0){
                ret = string.Format("{0}分{1}秒",t.Minutes,t.Seconds);
            }
            else{
                ret = string.Format("{0}秒",t.Seconds);
            }

            return ret;
        }

        public static string FormatTime_3(TimeSpan t){
            string ret = "";
            ret = string.Format("{0:D2}:{1:D2}:{2:D2}",t.Hours,t.Minutes,t.Seconds);
            return ret;
        }

        public static string GetTimeFormat2_1(int time)
        {
            float h = Mathf.FloorToInt(time / 3600f);
            float m = Mathf.FloorToInt(time / 60f - h * 60f);
            float s = Mathf.FloorToInt(time - m * 60f - h * 3600f);
            if (h > 0)
            {
                return h.ToString("00") + ":" + m.ToString("00") + ":" + s.ToString("00");
            }
            return m.ToString("00") + ":" + s.ToString("00");
        }
        
        public static string GetTimeFormat2_3(int time)
        {
            float h = Mathf.FloorToInt(time / 3600f);
            float m = Mathf.FloorToInt(time / 60f - h * 60f);
            float s = Mathf.FloorToInt(time - m * 60f - h * 3600f);
            return h.ToString("00") + ":" + m.ToString("00") + ":" + s.ToString("00");
        }

        public static string TimeSec2DateTime(long time)
        {
            DateTime startTime = TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc), TimeZoneInfo.Local);
            DateTime dt = startTime.AddSeconds(time);
            return dt.ToString("yyyy/MM/dd HH:mm:ss");
        }

        public static string GetCurrentRegion()
        {
            // 获取当前的 Android Java 环境
            AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
            AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");

            // 获取系统的资源对象
            AndroidJavaObject resources = currentActivity.Call<AndroidJavaObject>("getResources");
            // 获取配置对象
            AndroidJavaObject configuration = resources.Call<AndroidJavaObject>("getConfiguration");

            // 获取 Locale 对象
            AndroidJavaObject locale;
            if (Application.platform == RuntimePlatform.Android)
            {
                // 针对 Android 7.0 及以上版本
                AndroidJavaClass localeListClass = new AndroidJavaClass("java.util.LocaleList");
                AndroidJavaObject localeList = configuration.Get<AndroidJavaObject>("locales");
                locale = localeList.Call<AndroidJavaObject>("get", 0);
            }
            else
            {
                // 针对 Android 7.0 以下版本
                locale = configuration.Get<AndroidJavaObject>("locale");
            }

            // 获取国家代码
            string countryCode = locale.Call<string>("getCountry");
            return countryCode;
        }


        public static void ADCallBack(string type,Action cb){
            if(cb != null){
                cb();
            }
        }

        public static void ExitGame(){
#if UNITY_EDITOR
            UnityEditor.EditorApplication.isPlaying = false;
#else
            Application.Quit();
#endif
        }

        public static string GetPersistentPath_for_www()
        {
            string pre = "file://";
            #if UNITY_EDITOR || UNITY_STANDALONE_WIN
            pre = "file:///";
            #elif UNITY_ANDROID
            pre = "file://";
            #elif UNITY_IPHONE
            pre = "file://";
            #endif
            string path = pre + Application.persistentDataPath + "/";
            return path;
        }

        public static string GetStreamingPath_for_www()
        {
            string pre = "file://";
            #if UNITY_EDITOR
            pre = "file://";
            #elif UNITY_ANDROID
            pre = "";
            #elif UNITY_IPHONE
                pre = "file://";
            #endif

            string path = pre + Application.streamingAssetsPath + "/";

            return path;
        }

        public static Type GetTypeFromAssembly(string assemblyName, string typeName)
        {
            Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
            foreach (Assembly assembly in assemblies)
            {
                if (assembly.GetName().Name == assemblyName)
                {
                    Type type = assembly.GetType(typeName);
                    if (type != null)
                    {
                        return type;
                    }
                }
            }
            return null;
        }
        
        public static bool isEditorNoPlaying{
            get
            {
                bool y = false;
#if UNITY_EDITOR
            if(UnityEditor.EditorApplication.isPlaying == false)
                y = true;
#else
                y = false;
#endif

                return y;
            }
        }
    }
}
